import json

if __name__ == '__main__':

    content = []


    def split_list(lst, ratios, num_splits):
        """
        将列表按照指定比例和数量拆分成子列表
        :param lst: 待拆分列表
        :param ratios: 每个子列表的元素占比，由小数表示的列表
        :param num_splits: 子列表的数量
        :return: 拆分后的子列表组成的列表
        """
        if len(ratios) != num_splits:
            raise ValueError("The length of ratios must equal to num_splits.")
        total_ratio = sum(ratios)
        if total_ratio != 1:
            raise ValueError("The sum of ratios must be equal to 1.")
        n = len(lst)
        result = []
        start = 0
        for i in range(num_splits):
            end = start + int(n * ratios[i])
            result.append(lst[start:end])
            start = end
        return result


    with open('./data/clue/cluener_manual_dianpin_v3.json', 'r', encoding='utf-8') as f:
        for line in f.readlines():
            # json_line = json.loads(line.strip())
            content.append(line)
    res = split_list(content, [0.9, 0.1], 2)
    with open('./data/clue/train.json', 'w', encoding='utf-8') as f1:
        for item in res[0]:
            f1.write(item)

    with open('./data/clue/test.json', 'w', encoding='utf-8') as f2:
        for item in res[1]:
            f2.write(item)
    # with open('./data/clue/train.json', 'r', encoding='utf-8') as f1:
    #     for line in f1.readlines():
    #
    #         json_line = json.loads(line.strip())